package com.yyk.yiliao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yyk.yiliao.R;
import com.yyk.yiliao.bean.Pharmacy_Info;

import java.util.List;

/**
 * Created by ${zk} on 2018/7/22 0022.
 * <p>
 * <p>
 * 多布局插入广告 暂时还没有实现
 */

public class Pharmacy_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context             mContext;
    List<Pharmacy_Info> mList;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;


    public Pharmacy_Adapter(Context context, List<Pharmacy_Info> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_1) {
            View view = View.inflate(mContext, R.layout.adapter_item_pharmacy, null);
            return new MyViewHolder(view);
        } else if (viewType == TYPE_2) {
            View view2 = View.inflate(mContext, R.layout.adapter_item_pharmacy2, null);

            return new MyViewHolder2(view2);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder holder1 = null;
        MyViewHolder2 holder2 = null;

        if (holder instanceof MyViewHolder) {

            holder1 = (MyViewHolder) holder;
        } else if (holder instanceof MyViewHolder2) {
            holder2 = (MyViewHolder2) holder;

        }
        if (TYPE_1 == 0) {
            holder1.iv.setImageResource(mList.get(position).getImg());
            holder1.tv.setText(mList.get(position).getMiaoshu());
            holder1.tv2.setText(mList.get(position).getPrice());
        } else {
            holder2.iv.setImageResource(R.mipmap.img_ggw2);
        }
    }


    /**
     * 获取每个位置的View
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        int type = mList.get(position).type;
        if (type == 0) {
            return TYPE_1;
        } else if (type == 1) {
            return TYPE_2;
        }
        return 0;
    }


    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView  tv;
        private TextView  tv2;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
            tv2 = itemView.findViewById(R.id.tv2);
        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        private ImageView iv;

        public MyViewHolder2(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);

        }
    }


}
